/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Chitietsanpham;
import pojo.Sanpham;
import util.HibernateUtil;

/**
 *
 * @author sangnguyen
 */
public class ChiTietSanPhamDAO {
    public static Chitietsanpham getChiTietSanPham(int idCH, int idSP){
        Chitietsanpham ds = new Chitietsanpham();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;        
        transaction = (Transaction) session.beginTransaction();
        Query query = session.createQuery("Select ctsp from Chitietsanpham as ctsp inner join ctsp.sanpham as sp inner join ctsp.cuahang as ch where sp.idsp=:idSP and ch.idch=:maCH and ctsp.tinhtrang=1");
        query.setInteger("idSP", idSP);
        query.setInteger("maCH", idCH);
        transaction.commit();
        ds = (Chitietsanpham)query.uniqueResult();
        session.close();
        return ds;                
    }
     public static Chitietsanpham getChiTietSanPhamByID(int idCTSP){
        Chitietsanpham ds = new Chitietsanpham();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;        
        transaction = (Transaction) session.beginTransaction();
        ds = (Chitietsanpham)session.get(Chitietsanpham.class, idCTSP);
        transaction.commit();        
        session.close();
        return ds;                
    }
    public static boolean UpdateChitietSP(Chitietsanpham obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();    
        try{
            session.update(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();                            
        }
        return true;
    }
}
